django order by foreign key count

37

# order_by count of foreign key relation
from django.db.models import Count
top_users = User.objects.filter(problem_user=False) \
                .annotate(num_submissions=Count('submission')) \ # adds column "num_submissions" in "top_users"
                .order_by('-num_submissions')

Comments

Submit
0 Comments